package com.hy.admin.controller.pages;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hy.core.response.ServicesResult;

/**
 * Created by shitou on 17/12/26.
 */
@Controller
public class IndexController extends PageControllerBase{
    /**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "redirect:/page/admin/";
    }
    
    @RequestMapping(value="/logout", method=RequestMethod.GET)
	public String logout(Model model,HttpServletRequest request, HttpServletResponse response, 
			Authentication authentication) {
//		logout();
//		Assert.notNull(request, "HttpServletRequest required"); 
		HttpSession session = request.getSession(); 
		if (session != null) { 
//		logger.debug(“Invalidating session: ” + session.getId()); 
		//#1.使当前session失效 
			session.invalidate();
		}
        SecurityContext context = SecurityContextHolder.getContext();
        //#2.清空当前的`SecurityContext`
        context.setAuthentication(null);
	    SecurityContextHolder.clearContext();
		return "redirect:/";
	}
    
    @RequestMapping(value="/page/admin/logout", method=RequestMethod.GET)
	public String adminLogout(Model model) {
		logout();
		return "redirect:/page/admin/";
	}

    @RequestMapping(value="/page/teacher/logout", method=RequestMethod.GET)
	public String teacherLogout(Model model) {
//		logout();
    		HttpSession session = request.getSession(); 
    		if (session != null) { 
//    		logger.debug(“Invalidating session: ” + session.getId()); 
    		//#1.使当前session失效 
    		session.invalidate();
    		}
	        SecurityContext context = SecurityContextHolder.getContext();
	        //#2.清空当前的`SecurityContext`
	        context.setAuthentication(null);
    	    SecurityContextHolder.clearContext();
		return "redirect:/page/teacher/";
	}
    
    
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(Model model) {
    	saveDefaultAttributes2Model(model);
        return makeViewName("test");
    }
    
	@Override
	protected String getViewPrefix() {
		return "";
	}
	
	/**
	 * 清除session和SecurityContext
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/signout",produces="application/json;charset=UTF-8")
	public ServicesResult<Boolean> signout() {
		ServicesResult<Boolean> servicesResult=new ServicesResult<>();
		try {
			HttpSession session = request.getSession(); 
			if (session != null) { 
			//#1.使当前session失效 
			session.invalidate();
			}
			SecurityContext context = SecurityContextHolder.getContext();
			//#2.清空当前的`SecurityContext`
			context.setAuthentication(null);
			SecurityContextHolder.clearContext();
			servicesResult.setResult(true);
			return servicesResult;
		} catch (Exception e) {
			e.printStackTrace();
		}
		servicesResult.setResult(false);
		return servicesResult;
	}
	
}
